// pages/myOrder/myOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    no_pic:'/static/images/no-pic.png',
    isLogin:false,
    tabs: [{
      name: "全部"
    }, {
      name: "待付款"
    }, {
      name: "待服务"
    }, {
      name: "待验收"
    }, {
      name: "待评价"
    }],
    dataList:[],
    cate:0,
    pageIndex: 1,
    allDone:false,
    loadding: false,
    pullUpOn: true,
    scrollTop: 0
  },

 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      isLogin:wx.util.isLogin()
    })
    let cate=wx.getStorageSync("order_tab");
    if(cate){
      wx.removeStorageSync("order_tab");
      this.setData({
        cate:cate<0?0:cate
      })
    }
    this.reload();
  },
  loginPage(){
    wx.navigateTo({
      url:"/pages/login/login"
    })
  },
  orderAgin(e){
    let gg_id=e.currentTarget.dataset.ggid;
    wx.navigateTo({
      url:"/pages/submitOrder/submitOrder?gg_id="+gg_id
    })
  },
  async load(){
    if(this.data.allDone){
      return;
    }
    if(this.data.loadding){
      return;
    }
    this.setData({
      loadding: true
    })
    let res=null;
    try{
      res=await wx.util.post("order/rows",{page:this.data.pageIndex,cate:this.data.cate});
    }catch(e){
      this.setData({
        loadding: false
      })
      this.loadding=false;
      wx.stopPullDownRefresh();
    }
    wx.stopPullDownRefresh();
    if(!res){
      return;
    }
    if(res.data.data.length>0){
      this.setData({
        pageIndex: this.data.pageIndex++
      })
    }
    if(res.data.data.length==0 || res.data.data.length<res.data.per_page){
      this.setData({
        allDone: true
      })
    }
    this.setData({
      loadding: false,
      pullUpOn: false,
      dataList: this.data.dataList.concat(res.data.data)
    })
  },
  reload(){
    this.setData({
      allDone: false,
      pullUpOn: false,
      loadding: false,
      pageIndex: 1,
      dataList: []
    })
    this.load();
  },
  async del(e){
    let id=e.currentTarget.dataset.id;
    await wx.util.post("order/del",{id:id});
    wx.util.toast('删除成功');
    this.reload();
  },
  change(e) {
    this.setData({
      cate: e.detail.index
    })
    this.reload();
  },
  pj(e){
    let id=e.currentTarget.dataset.id;
    wx.util.href("/pages/my/pj?id="+id,true);
  },
  detail(e) {
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../orderDetail/orderDetail?id='+id
    })
  },
  invoiceDetail(){
    wx.util.href('../invoiceDetail/invoiceDetail')
  },
  addEvaluate(){
    wx.util.href('../addEvaluate/addEvaluate')
  },
  onPullDownRefresh() {
    this.reload();
  },
  onReachBottom: function() {
    this.load();
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop
    })
  }
})